<?php

namespace app\communal\controller;

use app\Param;
use app\model\SysSetting as ModelSysSetting;
use app\model\ItemType as ModelItemType;
use app\model\Package as ModelPackage;
use app\user\logic\Order as UserLogicOrder;
use app\logic\CaiNiao as LogicCaiNiao;

use think\Db;


class PublicParam extends Param
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 物品类型列表 \n
     * URI : /item.list
     * @param :
     *  name          |  type  | null | description
     * ---------------|--------|------|-------------
     *
     * @return
     *  name        |  type  | description
     * -------------|--------|-------------
     *  list        | array  | 列表
     *
     * list
     *  name        |  type  | description
     * -------------|--------|-------------
     *  itemTypeId  | string | 物品类型id
     *  itemName    | string | 标题
     *
     */
    public function item()
    {
        $list = ModelItemType::getInstance()->toSelect(['is_show' => 1], 'id as itemTypeId,item_name as itemName', null, null, 'sort asc');
        _returnData(['list' => $list]);
    }


    /**
     * 包装选项列表 \n
     * URI : /package.list
     * @param :
     *  name          |  type  | null | description
     * ---------------|--------|------|-------------
     *
     * @return
     *  name        |  type  | description
     * -------------|--------|-------------
     *  list        | array  | 列表
     *
     * list
     *  name        |  type  | description
     * -------------|--------|-------------
     *  name        | string | 包装类型
     *  packageId   | int    | 包装id
     *
     */
    public function packageList()
    {
        $list = ModelPackage::getInstance()->toSelect(['is_show' => 1], 'id as packageId,name');
        _returnData(['list' => $list]);
    }


    /**
     * 保率 \n
     * URI : /protect.rate
     * @param :
     *  name          |  type  | null | description
     * ---------------|--------|------|-------------
     *
     * @return
     *  name        |  type  | description
     * -------------|--------|-------------
     *  rate        | float  | 保率
     *
     */
    public function protectRate()
    {
        $rate = UserLogicOrder::getInstance()->protectRate();
        _returnData(['rate' => $rate]);
    }
    /**
     * 运费规则 \n
     * URI : /freight.rules
     * @param :
     *  name          |  type  | null | description
     * ---------------|--------|------|-------------
     *  passport      | string | 必填  | 登录凭证
     *
     * @return
     * list
     *  name          |  type  | description
     * ---------------|--------|-------------
     *  firstHeavyFee | float  | 首重金额
     *  nextHeavyFee  | float  | 次重金额
     *
     */
    public function heavyFeeRule()
    {
        $first_heavy_fee = UserLogicOrder::getInstance()->getFirstHeavyFee(); //首重

        $next_heavy_fee = UserLogicOrder::getInstance()->nextHeavyFee(); //首重

        _returnData(['firstHeavyFee' => $first_heavy_fee, 'nextHeavyFee' => $next_heavy_fee]);
    }

    /*
     * 平台logo
     * URI：/communal/platform.logo
     */
    public function getPlatformLogo()
    {
        $logo = ModelSysSetting::getInstance()->getValue('platform_logo');
        _returnData(['logo'=>$logo]);
    }
    /*
     * 协议
     * type:1：用户；2：快递；3：司机；4：站点
     * URI:/communal/platform.agreement
     */
    public function getAgreement()
    {
        $type = $this->_getContentParams('type');
//        $type = 1;
        switch ($type){
            case 1 :
                $key = 'membership_agreement';
                break;
            case 2 :
                $key = 'courier_agreement';
                break;
            case 3 :
                $key = 'driver_agreement';
                break;
            case 4 :
                $key = 'site_agreement';
                break;
            default:
                _returnError(10014);
                break;
        }
        $agreement = ModelSysSetting::getInstance()->getValue($key);
        _returnData(['agreement'=>$agreement]);
    }
    /*
     * 获取配置参数
     * URI:/communal/param.get
     */
    public function getSysValue()
    {
        $key = $this->_getContentParams('key');
        _toCompare(empty($key),'==',true,10014);
//        $key = 'cash_Pledge';
        $value = ModelSysSetting::getInstance()->toValue(['key'=>$key],'value');
        if ($value) _returnData(['value'=>$value]);
        _returnError(10014);
    }
    /*
     * 获取运单号
     * URI：get.express.number
     */
    public function getBill()
    {
        $obj = new \app\logic\CaiNiao();
        $expressSn = $obj->getWaybill('YTO');
        echo $expressSn;
    }

}
